home *** CD-ROM | disk | FTP | other *** search
- Path: news.cs.tut.fi!usenet
- From: p150650@kaarne.cs.tut.fi (Tero Pulkkinen)
- Newsgroups: comp.lang.c++
- Subject: templates and virtual functions
- Date: 13 Jan 1996 19:44:57 +0200
- Organization: Tampere University of Technology, Finland
- Sender: p150650@kaarne.cs.tut.fi
- Distribution: inet
- Message-ID: <xdu20p4kmzp.fsf@kaarne.cs.tut.fi>
- NNTP-Posting-Host: kaarne.cs.tut.fi
- NNTP-Posting-User: p150650
- X-Newsreader: Gnus v5.0.13
-
-
- I have a small problem with combining runtime information (maybe user
- selectable) and templates. Now it seems that at the interface where
- your code moves from handling pointers to a base class to an template
- instantiation you need to list all the possible template functions
- you can call from the interface. This looks like a big maintainance
- problem, and i'd like to find workaround. Virtual member function
- templates would solve the problem, but as you know from D&E there's
- a good reason C++ doesnt support those.
-
- so, to call a template function, you always need to supply
- the template parameter. Now i have a pointer to a base class of the
- that type, and i'd like to call a template function with the derived
- class as a template parameter.
-
- The basic problem i try to solve using templates is avoiding virtual
- function calls in the most critical parts of the code without rewriting
- all algorithms for every single type i have. I.e. I have few algorithms
- of drawing different kind shapes pixel by pixel to the screen. Now i also
- need to make all the drawing functions generic, so that it works with
- different kind of display devices, without rewriting the drawing algorithms.
- The problem comes when i cant afford to make virtual function call for
- every pixel. So, i have the drawing algorithms defined as follows:
-
- template <class Screen>
- void drawblock() {
- // now use Screen and Screen::iterator to draw a block to any screen
- // pixel by pixel.
- }
-
- Then i might have at the start of the program a detection of the display
- device or let the user select the display device to use. That selection
- gives me a pointer to a some ScreenDevice-base class. But how to
- call those template functions without listing all the algorithms and
- all different screen's somewhere in the program?
-
- The selection of the ScreenDevice is made at only one place at the start
- of the program. The selection must be runtime, but i still wouldnt like to
- have all functions virtual functions nor would i want to have all functions
- templates.
-
- The sollutions i've found:
-
- 1) Get rid of templates and use virtual functions only.
- - This is not working because of I cant afford virtual function call
- overhead for every pixel i draw to screen. Templates allow the
- critical part to be inlined, which makes it more than 30 times
- faster.
-
- 2) Make all the functions which can call any screen handling have screen
- as a template parameter. At the selection only have to call one template
- function that runs the whole program. (and only have to list the
- different screen devices once)
- - This is not too good, because alot of code is duplicated even if its
- not really necessary. Most code really doesnt draw pixels to screen,
- they just call other functions that make the drawing.
- (means all code is in template functions!)
- - It also makes the compile times really enormous.
- (btw. this is how i have it implemented right now:)
-
- 3) The third sollution is to make some kind of interface, which stores the
- selected screen device information and for every different algorithm
- make a virtual function call for different screen devices, and call te
- template function from that virtual function.
- - problem is that with this case i need to list all the algorithms in
- every screen device i have. This is a maintainance problem and i'm
- looking for better sollution. Though this is the only sollution
- i've found that's good enough for runtime efficency and for
- program size requirements. This is what i'm about to implement, if
- i cant figure any better sollution. (This is similar sollution than
- for example pascal's typecase to replace virtual functions, and it
- sure doesnt sound too good to me (=list everything in million places))
-
- So, if anyone got some way to solve the problem better, i'd like to hear
- them.
-
- -- Tero Pulkkinen -- terop@kotka.cs.tut.fi --
-
-
-
-
-
-
-
-
-
-